package com.c.ctgproject.ui.login

import android.util.Base64
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserver
import com.c.ctgproject.http.ObserverListener
import com.c.ctgproject.utils.CustomToast
import com.c.ctgproject.utils.MyLog
import kotlinx.android.synthetic.main.activity_change_password.*
import okhttp3.ResponseBody
import org.json.JSONObject

/**
 *作者:LiBW
 *创建日期: 2020/8/17
 *描述: 修改密码
 */
class ChangePasswordActivity : BaseActivity() {
    override fun getLayout(): Int {
        return R.layout.activity_change_password
    }

    override fun event() {
        changeTransparentStatusBar()
    }

    override fun click() {
        //提交按钮
        mConfirmRL.setOnClickListener {
            val oldPassword = mOldPasswordET.text.toString()
            val newPassword = mNewPasswordET.text.toString()
            val newPasswordAgain = mPasswordAgainET.text.toString()

            if (newPassword == newPasswordAgain) {
                val listener = object : ObserverListener {
                    override fun onNext(t: ResponseBody) {
                        val data = t.string().toString()
                        MyLog.d("修改密码$data")
                        val json = JSONObject(data)
                        if (json.getInt("status") == 0) {
                            CustomToast.showToast(this@ChangePasswordActivity, "修改密码成功")
                            finish()
                        } else {
                            CustomToast.showToast(this@ChangePasswordActivity, json.getString("msg"))
                        }
                    }

                    override fun onError(error: String) {
                        CustomToast.showToast(this@ChangePasswordActivity, error)
                    }
                }
                ApiMethods.changePassword(MyObserver(this, listener),
                    Base64.encodeToString(oldPassword.toByteArray(), Base64.DEFAULT),
                    Base64.encodeToString(newPassword.toByteArray(), Base64.DEFAULT))
            } else {
                CustomToast.showToast(this, "两次密码输入不一致,请重新输入")
            }
        }
    }

}